package com.shun.media.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shun.common.R;
import com.shun.media.mapper.GjydResourceInfoMapper;
import com.shun.media.service.IGjydResourceInfoService;
import com.shun.media.vo.GjydResourceInfo;
import com.shun.oss.common.FILE_TYPE;
import com.shun.oss.common.OSS_CTL_TYPE;
import com.shun.oss.common.OSS_SER_TYPE;
import com.shun.oss.common.RS_TYPE;
import com.shun.oss.service.OssExecuteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * <p>
 * 资源文件列表 服务实现类
 * </p>
 *
 * @author clever
 * @since 2021-10-18
 */
@Slf4j
@Service
public class GjydResourceInfoServiceImpl extends ServiceImpl<GjydResourceInfoMapper, GjydResourceInfo> implements IGjydResourceInfoService {

    private final static String UPLOAD_FILE_ERR = "上传文件失败！";

    private final static String DEL_FILE_ERR = "删除文件失败！";

    @Autowired
    private OssExecuteService ossExecuteService;

    public R addOssFile(MultipartFile file,RS_TYPE rsType,OSS_CTL_TYPE ctlType){
        log.debug("file文件大小：{}",file.getSize());
        String fileOriginalName = file.getOriginalFilename();
        String fileSuffix = StrUtil.sub(fileOriginalName,fileOriginalName.lastIndexOf("."),fileOriginalName.length());
        log.debug("上传文件名称：{},类型:{}",fileOriginalName,fileSuffix);
        String fileName = IdUtil.objectId()+fileSuffix;
        log.debug("新文件名称：{}",fileName);
        try{
            String filePath = getFilePath(fileName);
            GjydResourceInfo resourceInfo = new GjydResourceInfo();
            resourceInfo.setRsId(IdUtil.objectId());
            resourceInfo.setCreateUser(StpUtil.getLoginIdAsString( ));
            resourceInfo.setRsBucket(ossExecuteService.getBucketName());
            resourceInfo.setRsLocation(OSS_SER_TYPE.ALIYUN.code);
            resourceInfo.setRsOriginName(fileOriginalName);
            resourceInfo.setRsPath(filePath);
            resourceInfo.setRsSizeKb(FileUtil.readableFileSize(file.getSize()));
            resourceInfo.setRsType(rsType.getType());
            resourceInfo.setRsOssName(fileName);
            resourceInfo.setRsSuffix(fileSuffix);
            resourceInfo.setRsSize(file.getSize());
            resourceInfo.setSecretFlag(ctlType.code);
            boolean flag = ossExecuteService.uploadFile(filePath,file.getInputStream(), ctlType);
            log.debug("flag:{}",flag);
            if(flag) {
                boolean saveFlg = save(resourceInfo);
                return saveFlg?R.suc(resourceInfo.getRsId()):R.error(UPLOAD_FILE_ERR);
            }else{
                log.warn("上传OSS文件异常！");
                return R.error(UPLOAD_FILE_ERR);
            }
        }catch (Exception e){
            log.error(e.getLocalizedMessage());
            return R.error(UPLOAD_FILE_ERR);
        }
    }

    @Override
    public R delRsById(String rsById) {
        GjydResourceInfo resourceInfo = getById(rsById);
        boolean delFlag = ossExecuteService.delOssFile(resourceInfo.getRsPath());
        if(delFlag){
            return removeById(rsById)?R.suc():R.error(DEL_FILE_ERR);
        }
        return R.error(DEL_FILE_ERR);
    }

    @Override
    public R addPubImgOssFile(MultipartFile file){
        log.debug("file文件大小：{}",file.getSize());
        return addOssFile(file,RS_TYPE.IMG,OSS_CTL_TYPE.CTL_PUBLIC_READ);
    }

    @Override
    public R addPubVideoOssFile(MultipartFile file) {
        return addOssFile(file,RS_TYPE.VIDEO,OSS_CTL_TYPE.CTL_PUBLIC_READ);
    }

    @Override
    public R addPriImgOssFile(MultipartFile file){
        log.debug("file文件大小：{}",file.getSize());
        return addOssFile(file,RS_TYPE.IMG,OSS_CTL_TYPE.CTL_PRIVATE);
    }

    @Override
    public R addPriVideoOssFile(MultipartFile file) {
        return addOssFile(file,RS_TYPE.VIDEO,OSS_CTL_TYPE.CTL_PRIVATE);
    }

    /**
     * 获取文件路径，上传OSS
     * @param fileName
     * @return
     */
    public String getFilePath(String fileName){
        return getOssImgDir()+"/"+fileName;
    }

    /**
     * 获得OSS的图片存储路径
     * @return
     */
    public String getOssImgDir(){
        return FILE_TYPE.MEDIA.type+"/img/"+ DateUtil.format(new Date(),"yyyyMMdd");
    }

    /**
     * 获得OSS的视频存储路径
     * @return
     */
    public String getOssVideoDir(){
        return FILE_TYPE.MEDIA.type+"/video/"+ DateUtil.format(new Date(),"yyyyMMdd");
    }
}
